package com.shameit.goodqrinfomanager.ui.activity.login;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.shameit.goodqrinfomanager.MyApplication;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.bean.litepal.LoginUser;
import com.shameit.goodqrinfomanager.source.http.DataRepository;
import com.shameit.goodqrinfomanager.source.http.RequestBodyFactory;
import com.shameit.goodqrinfomanager.ui.activity.main.MainActivity;

import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class LoginViewModel extends BaseViewModel<DataRepository> {

    public ObservableField<String> username = new ObservableField<>("test");
    public ObservableField<String> password = new ObservableField<>("test");

    public MutableLiveData<LoginUser> loginUserMutableLiveData = new MutableLiveData<>();


    public LoginViewModel(@NonNull Application application, DataRepository model) {
        super(application, model);
    }

    public BindingCommand loginBtnCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    //    public View.OnClickListener loginOnclick = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            login();
//        }
//    };

    public void login() {
        //TODO login

        Bundle bundle = new Bundle();
        bundle.putString("username",username.get());
        MyApplication.login(new LoginUser(username.get(),password.get()));
        startActivity(MainActivity.class,bundle);
        finish();

//        HashMap hashMap = new HashMap();
//        //hashMap.put("token", "");
//        hashMap.put("username", username.get());
//        hashMap.put("password", password.get());
//        addSubscribe(model.login(RequestBodyFactory.getRequestBody(new Gson().toJson(hashMap))).
//                compose(RxUtils.Obs_io_main())
//                .compose(RxUtils.handle_result())
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
//                        showDialog();
//                    }
//                }).subscribe(new Consumer<LoginUser>() {
//                    @Override
//                    public void accept(LoginUser loginUser) throws Exception {
//                        dismissDialog();
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        dismissDialog();
//                        ToastUtils.showShort("失败!错误码："+throwable.getMessage());
//                        Bundle bundle = new Bundle();
//                        bundle.putString("username",username.get());
//                        MyApplication.login(new LoginUser(username.get(),password.get()));
//                        startActivity(MainActivity.class,bundle);
//                        finish();
//                    }
//                }));
    }
}
